package com.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Hyperlink;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Recommend_Me_Music implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	
	////////////////////////////////////////// API Info //////////////////////////////////////
	static String MY_API_KEY = "120608684706570";
	static String MY_APP_SECRET = "a6d5302ace93401b0ad02dab01e0141c";
	/////////////////////////////////////////API Info  //////////////////////////////////////
	
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		final String authToken = Location.getParameter("code");
	    final String error = Location.getParameter("error_reason");
	    final String OauthToken = Location.getParameter("access_token");
		
		
		//////////////////////////////////// Handle Second Step Of OAuth ////////////////////////////////////////
		if(error != null)
		{
			//lblInfo.setText("Error Occurred" + error);
		}
		
		if(OauthToken != null)
		{
			//lblInfo.setText("Access Token Received = "+OauthToken);
		}
		
		if(authToken != null)
		{
			//lblInfo.setText("We are gud to go");
			String AccessTokenURL = "https://graph.facebook.com/oauth/access_token?client_id="+MY_API_KEY+"&redirect_uri=https://recommendmemusic.appspot.com/&client_secret="+MY_APP_SECRET+"&code="+authToken;
			sendNameToServer(AccessTokenURL);
		}
		
		if(authToken == null)
		{
			Window.open("https://www.facebook.com/dialog/oauth?client_id=120608684706570&redirect_uri=https://recommendmemusic.appspot.com/&scope=publish_stream,offline_access,read_stream", "_self", null);
		}
		//////////////////////////////////// We If Are Authenticated We Will Fetch Data////////////////////////////
	}
	
	private void sendNameToServer(String textToServer) {
		greetingService.greetServer(textToServer,
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						//lblInfo.setText(SERVER_ERROR);
					}

					public void onSuccess(String result) {
						//lblInfo.setText("We Passed result = " + result);
						//showresults(result);
						Window.open("https://recommendmemusic.appspot.com/authenticate.html?access_token="+result, "_self", null);
					}
				});
	}
	
	private void showresults(String Token)
	{
		@SuppressWarnings("serial")
		SafeHtml h = new SafeHtml() {
			
			@Override
			public String asString() {
				return "<a href=\"http://www.google.co.in\" target=\"_blank\">HTML Help</a>";
			}
		};
		//hprlnkNewHyperlink.setHTML(h);
	}
	
}
